package com.woniu.springcrud.controller;

import com.woniu.springcrud.entity.Permission;
import com.woniu.springcrud.enums.ResponseStatusEnum;
import com.woniu.springcrud.service.PermissionService;
import com.woniu.springcrud.vo.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: 16327
 * @date: 2021/1/19 17:44
 * @description:
 */
@RestController
public class PermissionController {
    @Autowired
    PermissionService permissionService;
    @PostMapping("/permissions/list")
    public ResponseVo list(){
        List<Permission> all = permissionService.findAll();
        return ResponseVo.successNoMsgWithData(all);
    }
    @PostMapping("/permissions/save")
    public ResponseVo save(@RequestBody Permission permission){
        int i = permissionService.insertPermission(permission);
        if (i<1){
            return ResponseVo.errorDefaultMsg(ResponseStatusEnum.ERROR);
        }else {
            return ResponseVo.successNoMsgWithData(permission);
        }
    }
    @PostMapping("/permissions/update")
    public ResponseVo update(@RequestBody Permission permission){
        int i = permissionService.updatePermission(permission);
        if (i<1){
            return ResponseVo.errorDefaultMsg(ResponseStatusEnum.ERROR);
        }else {

            return ResponseVo.successNoData();
        }
    }
    @PostMapping("/permissions/delete")
    public ResponseVo delete(@RequestBody String pid){
        int i = permissionService.deletePermissionById(Integer.valueOf(pid));
        if (i<1){
            return ResponseVo.errorDefaultMsg(ResponseStatusEnum.ERROR);
        }else {

            return ResponseVo.successNoData();
        }
    }
}

